﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DIST.LR.ZHJG.Pgybc.WCF.Interface;
using DIST.LR.ZHJG.Pgybc.WCF.EntityClass;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Linq;
using System.ServiceModel;

namespace DIST.LR.ZHJG.Pgybc.WCF.Manager
{
    public class EntitiesManager<T>:IServiceManager<T>
    {
        public EntitiesManager()
        {

        }

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="infos">数据集合</param>
        [OperationContract]
        public virtual void Add(IList<T> infos)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                foreach (T info in infos)
                {
                    session.Save(info);
                    AddOp();
                }
                transaction.Commit();
            }
            catch (Exception)
            {

                transaction.Rollback();
            }
            finally 
            {
                session.Close();
            }
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="ids">删除数据集合</param>
        /// <returns>返回bool值</returns>
        [OperationContract]
        public virtual bool Delete(IList<T> infos)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                foreach (T info in infos)
                {
                    session.Delete(info);
                    DeleteOp(info);
                }
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                session.Close();
            }
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="infos">修改集合</param>
        /// <returns>返回bool值</returns>
        [OperationContract]
        public virtual bool Update(IList<T> infos)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                foreach (T info in infos)
                {
                    session.Update(info);
                    UpdateOp(info);
                }
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                session.Close();
            }
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="hsql">sql语句</param>
        /// <returns>查询结果集合</returns>
        [OperationContract]
        public virtual IList<T> Select(string hsql = "")
        {
            IList<T> list;
            ISession session = NHibernateHelper.Instance().GetSession();
            try
            {
                list = session.CreateQuery(hsql).List<T>();
                SelectOp();
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                session.Close();
            }
        }

        [OperationContract]
        public virtual void SelectOp()
        {

        }

        [OperationContract]
        public virtual void AddOp()
        {

        }

        private void DeleteOp(T info)
        {

        }

        [OperationContract]
        public virtual void UpdateOp(T info)
        {

        }
    }
}